//根据窗体的大小设置中间内容的高度
function findDimensions() {
	var height = window.innerHeight;
	var height = height - 50;
	var Conent = document.getElementById("Contename");
	if(Conent!=null){Conent.style.height = height + "px";}

}
findDimensions();
window.onresize = findDimensions;

$(function(){
	if($("#Contename")!=null){
		$("#Contename").niceScroll({cursorcolor:"#00F"});//美化滚动条
	    $(".am-offcanvas-bar").niceScroll({cursorcolor:"#00F"});//美化滚动条
	}
	
	//全选的实现
    $(".checkAll").click(function(){
        $(".ck").prop("checked", this.checked);
    });
    $(".ck").click(function(){
        var option = $(".ck");
        option.each(function(i){
            if (!this.checked) {
                $(".checkAll").prop("checked", false);
                return false;
            }
            else {
                $(".checkAll").prop("checked", true);
            }
        });
    });
	
  //ajax get请求
    $('.ajax-get').click(function(){
        var target;
        var that = this;
        if ( $(this).hasClass('confirm') ) {
            if(!confirm('确认要执行该操作吗?')){
                return false;
            }
        }
        if ( (target = $(this).attr('href')) || (target = $(this).attr('url')) ) {
            $.get(target).success(function(data){
            	var dataObj=data;
                if (dataObj.status==1) {
                    if (dataObj.url) {
                        updateAlert(dataObj.info + ' 页面即将自动跳转~','alert-success');
                    }else{
                        updateAlert(dataObj.info,'alert-success');
                    }
                    setTimeout(function(){
                        if (dataObj.url) {
                            location.href=dataObj.url;
                        }else if( $(that).hasClass('no-refresh')){
                            $('#top-alert').find('button').click();
                        }else{
                            location.reload();
                        }
                    },1500);
                }else{
                    updateAlert(dataObj.info);
                    setTimeout(function(){
                        if (dataObj.url) {
                            location.href=dataObj.url;
                        }else{
                            $('#top-alert').find('button').click();
                        }
                    },1500);
                }
            });

        }
        return false;
    });

    //ajax post submit请求
    $('.ajax-post').click(function(){
    	
        var target,query,form;
        var target_form = $(this).attr('target-form');
        var that = this;
        var nead_confirm=false;
        
        if( ($(this).attr('type')=='submit') || (target = $(this).attr('href')) || (target = $(this).attr('url')) ){
        	
            form = $('.'+target_form);

            if (form.get(0)==undefined){
            	return false;
            }else if ( form.get(0).nodeName=='FORM' ){
            	if(form.validator().data('amui.validator').isFormValid() === false){
            		return false;
            	}
                if ( $(this).hasClass('confirm') ) {
                    if(!confirm('确认要执行该操作吗?')){
                        return false;
                    }
                }
                if($(this).attr('url') !== undefined){
                	target = $(this).attr('url');
                }else{
                	target = form.get(0).action;
                }
                query = form.serialize();
            }else if( form.get(0).nodeName=='INPUT' || form.get(0).nodeName=='SELECT' || form.get(0).nodeName=='TEXTAREA') {
            	
                form.each(function(k,v){
                    if(v.type=='checkbox' && v.checked==true){
                        nead_confirm = true;
                    }
                })
                
                if(!nead_confirm){
                	updateAlert('未选中元素' ,'alert-success');
                	setTimeout(function(){
                        $('#top-alert').find('button').click();
                    },1000);
                	
                	return false;
                	
                }else if ($(this).hasClass('confirm') ) {
                    if(!confirm('确认要执行该操作吗?')){
                        return false;
                    }
                }
                query = form.serialize();
                
            }else{
                if ( $(this).hasClass('confirm') ) {
                    if(!confirm('确认要执行该操作吗?')){
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            
            $(that).addClass('am-disabled').attr('autocomplete','off').prop('am-disabled',true);
            $.post(target,query).success(function(data){
            	var dataObj=data;
                if (dataObj.status==1) {
                    if (dataObj.url) {
                        updateAlert(dataObj.info + ' 页面即将自动跳转~','alert-success');
                    }else{
                        updateAlert(dataObj.info ,'alert-success');
                    }
                    
                    setTimeout(function(){
                    	$(that).removeClass('am-disabled').prop('am-disabled',false);
                        if (dataObj.url) {
                            location.href=dataObj.url;
                        }else if( $(that).hasClass('no-refresh')){
                            $('#top-alert').find('button').click();
                        }else{
                            location.reload();
                        }
                    },1500);
                    
                }else{
                    updateAlert(dataObj.info);
                    setTimeout(function(){
                    	$(that).removeClass('am-disabled').prop('am-disabled',false);
                        if (dataObj.url) {
                            location.href=dataObj.url;
                        }else{
                            $('#top-alert').find('button').click();
                        }
                    },1500);
                }
            });
        }
        return false;
    });

	/**顶部警告栏*/
	var top_alert = $('#top-alert');
	top_alert.find('.close').on('click', function () {
		top_alert.removeClass('block').slideUp(200);
	});
	/**底部警告栏*/
	var bottom_alert = $('#bottom-alter');
	bottom_alert.find('.close').on('click', function () {
		bottom_alert.removeClass('block').slideUp(200);
	});
	window.bottomAlert = function (text,c) {
		text = text||'default';
		c = c||false;
		if ( text!='default' ) {
			bottom_alert.find('.alert-content').text(text);
			if (bottom_alert.hasClass('block')) {
				
			} else {
				bottom_alert.addClass('block').slideDown(200);
			}
		} else {
			if (bottom_alert.hasClass('block')) {
				bottom_alert.removeClass('block').slideUp(200);
			}
		}
		if ( c!=false ) {
			bottom_alert.removeClass('alert-error alert-warn alert-info alert-success').addClass(c);
		}
	};
    window.updateAlert = function (text,c) {
		text = text||'default';
		c = c||false;
		if ( text!='default' ) {
            top_alert.find('.alert-content').text(text);
			if (top_alert.hasClass('block')) {
				
			} else {
				top_alert.addClass('block').slideDown(200);
			}
		} else {
			if (top_alert.hasClass('block')) {
				top_alert.removeClass('block').slideUp(200);
			}
		}
		if ( c!=false ) {
            top_alert.removeClass('alert-error alert-warn alert-info alert-success').addClass(c);
		}
	};
	
	
	
});



//选中指定的内容后颜色改变
$("ul[name=ullist]>li>a").click(function(){
    if ($(this).hasClass("select")) {
        $(this).addClass("un_select").removeClass("select");
    }else {
        $(this).addClass("select").removeClass("un_select");
    }
});


/**
 * 将 num 转换成数字
 * @param num
 * @param type
 * @returns
 */
function commafyback(num,type)
{
	num = num + '';
	var x = num.split(',');
	if(type)
	{
		//在计算控件那里需要添加特殊处理，如果返回值为空，不能返回NaN，所以，空的时候转换成0。后来字母也当成0处理
		for(var i = 0; i<x.length; i++){
			if(x[i]=="" || isNaN(parseFloat(x[i]))){
				x[i] = 0;
			}
		}
	}
	//找不到修改的原因，先再改回来
	//return (x.join(""));
	return parseFloat(x.join(""));
}

//清空模态框值
$(document).on('click', '.clear_modal', function(){
	
	var targetID = $(this).attr('target');
	if(targetID){
		
		$("#" + targetID).val('');
		$("#" + targetID + "_name").val('');
	}
	
});

//上传附件类型检测
var uploadify_onSelectError = function(file, errorCode, errorMsg) {  
    var msgText = "上传失败\n";  
    switch (errorCode) {  
        case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:  
            //this.queueData.errorMsg = "每次最多上传 " + this.settings.queueSizeLimit + "个文件";  
            msgText += "每次最多上传 " + this.settings.queueSizeLimit + "个文件";  
            break;  
        case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:  
            msgText += "文件大小超过限制( " + this.settings.fileSizeLimit + " )";  
            break;  
        case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:  
            msgText += "文件大小为0";  
            break;  
        case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:  
            msgText += "文件格式不正确，仅限 " + this.settings.fileTypeExts;  
            break;  
        default:  
            msgText += "错误代码：" + errorCode + "\n" + errorMsg;  
    }  
    this.queueData.errorMsg = msgText;  
};  


var uploadify_onUploadError = function(file, errorCode, errorMsg, errorString) {  
    // 手工取消不弹出提示  
    if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED  
            || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED) {  
        return;  
    }  
    var msgText = "上传失败\n";  
    switch (errorCode) {  
        case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:  
            msgText += "HTTP 错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:  
            msgText += "上传文件丢失，请重新上传";  
            break;  
        case SWFUpload.UPLOAD_ERROR.IO_ERROR:  
            msgText += "IO错误";  
            break;  
        case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:  
            msgText += "安全性错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:  
            msgText += "每次最多上传 " + this.settings.uploadLimit + "个";  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:  
            msgText += errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:  
            msgText += "找不到指定文件，请重新操作";  
            break;  
        case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:  
            msgText += "参数错误";  
            break;  
        default:  
            msgText += "文件:" + file.name + "\n错误码:" + errorCode + "\n"  
                    + errorMsg + "\n" + errorString;  
    }  
    this.queueData.errorMsg = msgText;  
}  
